<?php

namespace App\Providers;

use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Models\Menu;
use App\Models\Access;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any application authentication / authorization services.
     *
     * @param  \Illuminate\Contracts\Auth\Access\Gate  $gate
     * @return void
     */
    public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);
         //授权
        $gate->define('chkaccess', function ($user,$action) {
            $admin = \Auth::user();
            $routeinfo=Menu::where('level',3)->where('action',$action)->first();
            if($routeinfo){
                $ct=Access::where('role_id',$admin->role_id)->where('menu_id',$routeinfo->id)->count();
                if(!$ct){
                    return false;
                }
                else
                {
                    return true;
                } 
            }
        });
        //
    }
}
